﻿using System;
using CID.Execution.Controller.DeploymentInitiation;
using CID.Execution.Support.DeploymentInitiation;
using NUnit.Framework;

namespace CID.Execution.Controller.Tests.DeploymentInitiation
{
    [TestFixture]
    class WebPackageRegistrationServiceTests : BaseControllerTests
    {
        private WebDeploymentInitiationService webDeploymentInitiationService;

        protected override void ConfigureNails(NailsFramework.Config.INailsConfigurator nails)
        {
            nails.IoC.Lemming<Controller>(x => x.Ignore())
                .IoC.Lemming<DeploymentInitiatorStub>();
            base.ConfigureNails(nails);
        }

        public override void SetUp()
        {
            base.SetUp();
            
            DeploymentInitiatorStub.CleanFlags();
            webDeploymentInitiationService = new WebDeploymentInitiationService();
        }

        [Test]
        public void TestInitiateDeploymentByIds_ShouldCallDeploymentInitiatorInitiateDeploymentByIds()
        {
            webDeploymentInitiationService.InitiateDeploymentByIds(DevelopmentData.ActiveRelease.Id, OperationsData.Environment.Id, UsersData.Operator.Id);

            Assert.IsTrue(DeploymentInitiatorStub.InitiateDeploymentByIdsCalled);
            Assert.IsFalse(DeploymentInitiatorStub.InitiateDeploymentByNamesCalled);
        }


        [Test]
        public void TestInitiateDeploymentByNames_ShouldCallDeploymentInitiatorInitiateDeploymentByNames()
        {
            webDeploymentInitiationService.InitiateDeploymentByNames(DevelopmentData.ActiveRelease.Name, OperationsData.Environment.Name, UsersData.Operator.UserName);

            Assert.IsTrue(DeploymentInitiatorStub.InitiateDeploymentByNamesCalled);
            Assert.IsFalse(DeploymentInitiatorStub.InitiateDeploymentByIdsCalled);
        }
    }

    class DeploymentInitiatorStub : IDeploymentInitiator
    {
        public static bool InitiateDeploymentByIdsCalled { get; private set; }
        public static bool InitiateDeploymentByNamesCalled { get; private set; }

        public static void CleanFlags()
        {
            InitiateDeploymentByIdsCalled = InitiateDeploymentByNamesCalled = false;
        }

        public Guid InitiateDeployment(int releaseId, int targetEnvironmentId, int responsibleId)
        {
            InitiateDeploymentByIdsCalled = true;
            return Guid.Empty;
        }

        public Guid InitiateDeployment(string releaseName, string targetEnvironmentName, string responsibleUserName)
        {
            InitiateDeploymentByNamesCalled = true;
            return Guid.Empty;
        }
    }

}
